//
//  LoginManager.m
//  AAIM
//
//  Created by awh on 2019/11/24.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "LoginManager.h"
#import "AppDelegate.h"
#import "IMClient.h"
//#import "UserCenterRequestLogic.h"
//#import "LoginRequestLogic.h"
#import "ConversationManager.h"
@implementation LoginManager
SingleM(LoginManager);
- (void)setUp {
    _userModel = [self getLoginCustomer];
}
- (AppDelegate *)appDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)isLogin {
    UserModel * login_model =[self getLoginCustomer];
    if ([NSObject ldy_isEmpty:login_model.token]) {
        return NO;
    } else {
        return YES;
    }
}
-(void)loadData
{
    
}

- (void)loginMethod {
    [self  loadData];
    //连接socket
    if ([self isLogin]) {
        [[LiftIMClient sharedClient] connectWithUserid:1];
        [self iniUserDataBaseWtihType:1];
        [self pushHomeViewController];
    }else
    {
        [self pushLoginViewController];
    }
}
-(void)iniUserDataBaseWtihType:(NSInteger)type
{
    _userDataBase=[[UserDataBaseTool alloc]init];
}
-(void)initController
{
    _loginVC=[[LoginViewController alloc] init];
    _mainTabBar=[[MainTabBarController alloc]init];
}
/**
 退出系统
 */
- (void)loginOut
{
    [self removeLoginCustomer];
    //    [self removeLoginCustomer];
    [self initController];
    //    _loginVC = [[LoginViewController alloc] init];
    //    //    loginVC.navigationController.navigationBarHidden=YES;
    _firstNav = [[MainNC alloc] initWithRootViewController:_loginVC];
    self.appDelegate.window.rootViewController = _firstNav;
    
}

/**
 保存用户登录数据
 @return YES or NO
 */
- (BOOL)saveLoginCustomer:(UserModel *)userModel
{
    BOOL result = [CacheTool savePlist:@"cust.plist" dataObj:[userModel keyValues]];
    if (result) {
        _userModel = [self getLoginCustomer];
    }
    return result;
    
}


/**
 读取用户登录数据
 
 @return 顾客数据模型
 */
- (UserModel *)getLoginCustomer
{
    
    NSMutableDictionary *dict = (NSMutableDictionary *)[CacheTool readPlist:@"cust.plist" dataClass:[NSMutableDictionary class]];
    if (dict) {
        _userModel = [UserModel mj_objectWithKeyValues:dict];
    }
    return _userModel;
}
/**
 删除用户登录数据
 */
- (void)removeLoginCustomer
{
    UserModel * model = [[UserModel alloc] init];
    [self saveLoginCustomer:model];
}


/**
 进入引导页
 */
- (void)pushGuideViewController

{
    
}

/**
 进入登录页
 */
- (void)pushLoginViewController
{
    [self initController];
//        LoginViewController *loginVC = [[LoginViewController alloc] init];
    //    loginVC.navigationController.navigationBarHidden=YES;
    MainNC  *firstNav = [[MainNC alloc] initWithRootViewController:_loginVC];
    //    firstNav.navigationController.navigationBarHidden = YES;
    self.appDelegate.window.rootViewController = firstNav;
    
}

/**
 进入首页
 */
- (void)pushHomeViewController
{
    [self initController];
    //    _mainTabBar = [[MainTabBarController alloc] init];
    [[ConversationManager shareManager] reloadData];
    self.appDelegate.window.rootViewController = _mainTabBar;
}

/* 检查新版版本
-(NSInteger)compareVersion:(NSString *)version1 toVersion:(NSString *)version2
{
    NSArray *list1 = [version1 componentsSeparatedByString:@"."];
    NSArray *list2 = [version2 componentsSeparatedByString:@"."];
    for (int i = 0; i < list1.count || i < list2.count; i++)
    {
        NSInteger a = 0, b = 0;
        if (i < list1.count) {
            a = [list1[i] integerValue];
        }
        if (i < list2.count) {
            b = [list2[i] integerValue];
        }
        if (a > b) {
            return 1;//version1大于version2
        } else if (a < b) {
            return -1;//version1小于version2
        }
    }
    return 0;//version1等于version2
}*/

@end
